package cn.edu.scau.cmi.liangzijian.abstractFactory;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.liangzijian.abstractFactory.Meat.Meat;
import cn.edu.scau.cmi.liangzijian.abstractFactory.factory.AonongFactory;
import cn.edu.scau.cmi.liangzijian.abstractFactory.factory.Factory;
import cn.edu.scau.cmi.liangzijian.abstractFactory.factory.TianbangFactory;
import cn.edu.scau.cmi.liangzijian.abstractFactory.factory.WensFactory;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;


public class AbstractFactoryUIController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ComboBox<String> factoryTypeComboBox;

    @FXML
    private ComboBox<String> meatTypeComboBox;

    @FXML
    private Button product;

    @FXML
    private TextArea screen;

    private String factoryType = "";
    private String meatType = "";
    
    
    private Factory factory = null;

    
    @FXML
    void product(MouseEvent event) {
    	StringBuilder sb = new StringBuilder();
    	
    	if(factoryType.equals("Wens")) {
    		factory = new WensFactory();
    	} else if(factoryType.equals("Tianbang")) {
    		factory = new TianbangFactory();
    	} else if(factoryType.equals("Aonong")) {
    		factory = new AonongFactory();
    	}
    	
    	sb.append(factory.getName());
    	sb.append("--->");
    	
    	Meat meat = null;
    	if(meatType.equals("beef")) {
    		meat = factory.productBeef();
    	} else if(meatType.equals("pork")) {
    		meat = factory.productPork();
    	} else if(meatType.equals("chicken")) {
    		meat = factory.productChicken();
    	}
    	
    	sb.append(meat.getName());
    	sb.append("\n");
    	sb.append(meat.cook());
    	sb.append("\n\n");
    	
    	screen.appendText(sb.toString());
    }

    @FXML
    void initialize() {
        assert factoryTypeComboBox != null : "fx:id=\"factoryTypeComboBox\" was not injected: check your FXML file 'AbstractFactoryUI.fxml'.";
        assert meatTypeComboBox != null : "fx:id=\"meatTypeComboBox\" was not injected: check your FXML file 'AbstractFactoryUI.fxml'.";
        assert product != null : "fx:id=\"product\" was not injected: check your FXML file 'AbstractFactoryUI.fxml'.";
        assert screen != null : "fx:id=\"screen\" was not injected: check your FXML file 'AbstractFactoryUI.fxml'.";

        factoryTypeComboBox.setItems(FXCollections.observableArrayList("Wens", "Tianbang", "Aonong"));
        meatTypeComboBox.setItems(FXCollections.observableArrayList("beef", "pork", "chicken"));
        
        factoryTypeComboBox.valueProperty().addListener(new ChangeListener() {
        	@Override
        	public void changed(ObservableValue observable, Object oldValue, Object newValue) {
        		factoryType = (String)newValue;
        	}
        	
        });
        
        meatTypeComboBox.valueProperty().addListener(new ChangeListener() {
        	@Override
        	public void changed(ObservableValue observable, Object oldValue, Object newValue) {
        		meatType = (String)newValue;
        	}
        });
    }

}
